package com.meng.menggaga.ui;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;

import com.github.johnpersano.supertoasts.SuperActivityToast;
import com.github.johnpersano.supertoasts.SuperToast;
import com.meng.menggaga.R;
import com.meng.menggaga.util.CommonParameter;

public abstract class BaseActivity extends SwipeBackActivity {
    private SwipeBackLayout mSwipeBackLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(layoutResId());

        // set home as up button enable
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mSwipeBackLayout = getSwipeBackLayout();

        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_ALL);
        mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
            @Override
            public void onScrollStateChange(int state, float scrollPercent) {}

            @Override
            public void onEdgeTouch(int edgeFlag) {}

            @Override
            public void onScrollOverThreshold() {}
        });

        // getSupportActionBar().

        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(getResources().getColor(CommonParameter.sStyleBgColor)));

        initViews();
        initEvents();

        onLoadDatas();
    }

    protected void onLoadDatas() {}

    /**
     * 初始化布局
     */
    protected void initViews() {}

    /**
     * 初始化事件
     */
    protected void initEvents() {}

    protected abstract int layoutResId();

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition();
        JPushInterface.onPause(this);
    }

    /**
     * {@link SwipeBackLayout}
     * 
     * @param swipeBackType
     */
    protected void setEdgeTrackingEnable(int swipeBackType) {
        mSwipeBackLayout.setEnabled(true);
        mSwipeBackLayout.setEdgeTrackingEnabled(swipeBackType);
    }

    protected void setSwipeBackLayoutEnable(boolean enabled) {
        mSwipeBackLayout.setEnabled(enabled);
    }

    protected void setActionBarTitle(String title) {
        if (null != getSupportActionBar()) {
            getSupportActionBar().setTitle(title);
        }
    }

    protected void setActionBarTitle(int title) {
        if (null != getSupportActionBar()) {
            getSupportActionBar().setTitle(title);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    protected final void overridePendingTransition() {
        overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
    }

    protected void showStandardToast(int resId) {
        showStandardToast(getString(resId));
    }

    /**
     * 显示标准的toast
     * 
     * @param str
     */
    protected void showStandardToast(String text) {
        SuperActivityToast superToast = new SuperActivityToast(this, SuperToast.Type.STANDARD);
        superToast.setAnimations(SuperToast.Animations.SCALE);
        superToast.setDuration(SuperToast.Duration.SHORT);
        superToast.setBackground(CommonParameter.sStyleBgColor);
        superToast.setTextSize(SuperToast.TextSize.MEDIUM);
        superToast.setTextColor(getResources().getColor(CommonParameter.sStyleTextViewColor));
        superToast.setText(text);
        superToast.show();
    }

    protected SuperActivityToast showProgressToast(String text) {
        SuperActivityToast superToast =
                new SuperActivityToast(this, SuperToast.Type.PROGRESS_HORIZONTAL);
        superToast.setAnimations(SuperToast.Animations.SCALE);
        superToast.setDuration(SuperToast.Duration.SHORT);
        superToast.setBackground(CommonParameter.sStyleBgColor);
        superToast.setTextSize(SuperToast.TextSize.MEDIUM);
        superToast.setTextColor(getResources().getColor(CommonParameter.sStyleTextViewColor));
        superToast.setText(text);
        superToast.show();
        return superToast;
    }
}
